<?php

namespace app\admin\controller;

use app\admin\common\Common;
use app\admin\model\AdminModel;
use app\admin\validate\AdminValidate;
use think\Controller;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\exception\PDOException;
use think\Request;

class Login extends Controller
{

    /**
     * 返回登录界面和后台登录请求
     */
    public function index(Request $request){
        if ($request->isPost()){
                $data = $request->post();
                $validate = new AdminValidate();
                if (!$validate->scene('login')->check($data)) {
                    return Common::result(400,$validate->getError());
                }
            try {
                return AdminModel::login($data);
            } catch (DataNotFoundException $e) {
                 return Common::result(400,$e->getMessage());
            } catch (ModelNotFoundException $e) {
                 return Common::result(400,$e->getMessage());
            } catch (PDOException $e) {
                 return Common::result(400,$e->getMessage());
            } catch (DbException $e) {
                 return Common::result(400,$e->getMessage());
            } catch (Exception $e) {
                 return Common::result(400,$e->getMessage());
            }
        }else{
            return $this->fetch();
        }
    }



}